#!/usr/bin/env python
# -*- coding: UTF-8 -*-

from rest_framework.permissions import BasePermission


class IsXiaoMingPermission(BasePermission):

    def has_permission(self, request, view):
        """
        视图权限
        返回结果为True则表示允许访问视图类
        request:本次客户端提交的请求对象
        view:本次客户端访问的视图类
        """
        #
        username = request.query_params.get("user")
        return bool(username == "xiaoming")

    # def has_object_permission(self, request, view, obj):
    #     """
    #     模型权限，写了视图权限has_permission方法，一般不需要写这个了
    #     返回结果为True，则表示袁旭操作模型对象
    #     request：客户端请求对象
    #     view：本次客户端请求的视图对象
    #     obj：本次权限判断的模型对象
    #     """
    #     from school.models import Student
    #     if isinstance(obj, Student):
    #         # 限制只有小明才能操作Student模型
    #         username = request.query_params.get("user")
    #     return bool(username == "xiaoming")
